﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
                    xmlns:cdg="clr-namespace:cmdr.WpfControls.CustomDataGrid"
                    xmlns:prim="clr-namespace:System.Windows.Controls.Primitives">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="CellTemplate.xaml"/>
        <ResourceDictionary Source="RowTemplate.xaml"/>
    </ResourceDictionary.MergedDictionaries>


    <!--Control colors.-->
    <Color x:Key="ControlLightColor">White</Color>
    <Color x:Key="ControlMediumColor">#FF7381F9</Color>
    <Color x:Key="ControlDarkColor">#FF211AA9</Color>

    <Color x:Key="ControlMouseOverColor">#FF3843C4</Color>
    <Color x:Key="ControlPressedColor">#FF211AA9</Color>

    <Color x:Key="GlyphColor">#FF444444</Color>

    <!--Border colors-->
    <Color x:Key="BorderLightColor">#FFCCCCCC</Color>
    <Color x:Key="BorderMediumColor">#FF888888</Color>
    <Color x:Key="BorderDarkColor">#FF444444</Color>


    <!--Style and template for the resize control on the DataGridColumnHeader.-->
    <Style x:Key="ColumnHeaderGripperStyle"
       TargetType="{x:Type Thumb}">
        <Setter Property="Width" Value="8" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Cursor" Value="SizeWE" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--Style and template for the DataGridColumnHeader.-->
    <Style x:Key="ColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Background" Value="#807D7D"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                    <Grid x:Name="ColumnHeader">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="00:00:0.1" />
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="HighlightVisualElement" Storyboard.TargetProperty="Opacity" Duration="0" To="0.5"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SortStates" >
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="00:00:0.1" />
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Unsorted" />
                                <VisualState x:Name="SortAscending">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="SortIcon" 
                                           Storyboard.TargetProperty="Opacity" 
                                           Duration="0" To="1.0" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="SortDescending">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="SortIcon" 
                                          Storyboard.TargetProperty="Opacity" 
                                          Duration="0" To="1.0" />
                                        <DoubleAnimation 
                                          Storyboard.TargetName="SortIconTransform" 
                                          Storyboard.TargetProperty="ScaleY" 
                                          Duration="0" To="-1" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>

                        <Border x:Name="columnHeaderBorder" Padding="3" 
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                        </Border>

                        <Path Name="SortIcon" HorizontalAlignment="Right" 
                              VerticalAlignment="Center" Opacity="0" 
                              Stretch="Uniform" Width="8" 
                              Data="F1 M -5,5 L 5,5 L 0,0 Z"
                              Fill="{TemplateBinding Foreground}">
                            <Path.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform x:Name="SortIconTransform" CenterY="2"/>
                                </TransformGroup>
                            </Path.RenderTransform>
                        </Path>

                        <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" Style="{StaticResource ColumnHeaderGripperStyle}" />
                        <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{StaticResource ColumnHeaderGripperStyle}" />

                        <Border x:Name="HighlightVisualElement" IsHitTestVisible="False" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Background="White" Opacity="0"/>                    
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderBrush">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="{StaticResource BorderLightColor}" Offset="0" />
                    <GradientStop Color="{StaticResource BorderDarkColor}" Offset="1" />
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>

    <!--Style and template for the DataGridColumnHeadersPresenter.-->
    <Style x:Key="ColumnHeadersPresenterStyle" TargetType="{x:Type DataGridColumnHeadersPresenter}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeadersPresenter}">
                    <Grid>
                        <DataGridColumnHeader x:Name="PART_FillerColumnHeader" IsHitTestVisible="False" />
                        <ItemsPresenter />
                        <Border x:Name="DisabledVisualElement" IsHitTestVisible="False" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Background="#FFF4F4F4" Opacity="0"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="DisabledVisualElement" Property="Opacity" Value="0.8"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


    <ControlTemplate x:Key="customDataGridTemplate" TargetType="DataGrid">
        <ControlTemplate.Resources>
            <!--Style and template for the DataGridCell.-->
            <Style TargetType="{x:Type DataGridCell}">
                <Setter Property="Template" Value="{StaticResource CellTemplate}"/>
                <Setter Property="FocusVisualStyle">
                    <Setter.Value>
                        <Style>
                            <Setter Property="Control.Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <Rectangle Margin="-2" StrokeThickness="1" Stroke="#F7747B"/>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Setter Property="IsSelected" Value="True"/>
                        <Setter Property="BorderThickness" Value="0"/>
                    </Trigger>
                </Style.Triggers>
            </Style>

            <!--Style and template for the DataGridRow.-->
            <Style TargetType="{x:Type DataGridRow}">
                <Setter Property="SnapsToDevicePixels" Value="true" />
                <Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
                <Setter Property="ValidationErrorTemplate">
                    <Setter.Value>
                        <ControlTemplate>
                            <TextBlock Foreground="Red" Margin="2,0,0,0" Text="!" VerticalAlignment="Center" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="Template" Value="{StaticResource RowTemplate}" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsHighlighted}" Value="True">
                        <Setter Property="Background" Value="{DynamicResource Cmdr.HighlightLevel2BrushKey}"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ControlTemplate.Resources>

        <Border x:Name="border"
                SnapsToDevicePixels="True"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="0"
                Padding="{TemplateBinding Padding}">
            <Border.Background>
                <SolidColorBrush Color="{StaticResource ControlLightColor}" />
            </Border.Background>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Disabled">
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetName="border"
                                                          Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlLightColor}" />
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Normal" />
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <ScrollViewer x:Name="DG_ScrollViewer"
                          Focusable="False"
                          CanContentScroll="True"
                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                <ScrollViewer.Template>
                    <ControlTemplate TargetType="{x:Type ScrollViewer}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>

                            <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter"
                                                            Grid.Column="1"
                                                            Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                                            Style="{StaticResource ColumnHeadersPresenterStyle}"/>

                            <ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
                                                    Grid.ColumnSpan="2"
                                                    Grid.Row="1"
                                                    CanContentScroll="{TemplateBinding CanContentScroll}" />

                            <ScrollBar x:Name="PART_VerticalScrollBar"
                                       Grid.Column="2"
                                       Grid.Row="1"
                                       Orientation="Vertical"
                                       ViewportSize="{TemplateBinding ViewportHeight}"
                                       Maximum="{TemplateBinding ScrollableHeight}"
                                       Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                                       Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/>

                            <Grid Grid.Column="1" Grid.Row="2">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>

                                <ScrollBar x:Name="PART_HorizontalScrollBar"
                                           Grid.Column="1"
                                           Orientation="Horizontal"
                                           ViewportSize="{TemplateBinding ViewportWidth}"
                                           Maximum="{TemplateBinding ScrollableWidth}"
                                           Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
                                           Value="{Binding HorizontalOffset, Mode=OneWay, 
                                           RelativeSource={RelativeSource TemplatedParent}}"/>
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </ScrollViewer.Template>
                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
            </ScrollViewer>
        </Border>
    </ControlTemplate>

</ResourceDictionary>